package com.ym521.skeleton.core;

import android.widget.BaseAdapter;
import android.widget.ListView;

import androidx.annotation.ArrayRes;
import androidx.annotation.ColorRes;
import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import androidx.core.content.ContextCompat;

import com.ym521.skeleton.R;
import com.ym521.skeleton.adapter.SkeletonBaseAdapter;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/8/6
 * explain:
 */
public final class ListViewSkeletonScreen implements SkeletonScreen {

    private final ListView mListView;
    private final SkeletonBaseAdapter mSkeletonBaseAdapter;
    private BaseAdapter mBaseAdapter;

    private ListViewSkeletonScreen(Builder builder) {
        mListView = builder.listView;
        mBaseAdapter = builder.mBaseAdapter;
        mSkeletonBaseAdapter = new SkeletonBaseAdapter();
        mSkeletonBaseAdapter.setItemCount(builder.mItemCount);
        mSkeletonBaseAdapter.setLayoutReference(builder.mItemResID);
        mSkeletonBaseAdapter.setArrayOfLayoutReferences(builder.mItemsResIDArray);
        mSkeletonBaseAdapter.shimmer(builder.mShimmer);
        mSkeletonBaseAdapter.setShimmerColor(builder.mShimmerColor);
        mSkeletonBaseAdapter.setShimmerAngle(builder.mShimmerAngle);
        mSkeletonBaseAdapter.setShimmerDuration(builder.mShimmerDuration);
    }

    @Override
    public void show() {
        mListView.setAdapter(mSkeletonBaseAdapter);
    }

    @Override
    public void hide() {
        if (mBaseAdapter != null) {
            mListView.setAdapter(mBaseAdapter);
        }
    }

    /**
     * 可以多回调
     * 只有第一次有效
     */
    @Override
    public void dismiss() {
        if (mBaseAdapter != null) {
            mListView.setAdapter(mBaseAdapter);
            mBaseAdapter = null;
        }
    }

    public static class Builder {
        private BaseAdapter mBaseAdapter;
        private final ListView listView;
        private boolean mShimmer = true;
        private int mItemCount = 10;
        private int mItemResID = R.layout.layout_default_item_skeleton;
        private int[] mItemsResIDArray;
        private int mShimmerColor;
        private int mShimmerDuration = 1000;
        private int mShimmerAngle = 20;

        public Builder(ListView listView) {
            this.listView = listView;
            this.mShimmerColor = ContextCompat.getColor(listView.getContext(), R.color.shimmer_color);
        }

        /**
         * @param adapter the target recyclerView actual adapter
         */
        public Builder adapter(BaseAdapter adapter) {
            this.mBaseAdapter = adapter;
            return this;
        }

        /**
         * @param itemCount the child item count in recyclerView
         */
        public Builder count(int itemCount) {
            this.mItemCount = itemCount;
            return this;
        }

        /**
         * @param shimmer whether show shimmer animation
         */
        public Builder shimmer(boolean shimmer) {
            this.mShimmer = shimmer;
            return this;
        }

        /**
         * the duration of the animation , the time it will take for the highlight to move from one end of the layout
         * to the other.
         *
         * @param shimmerDuration Duration of the shimmer animation, in milliseconds
         */
        public Builder duration(int shimmerDuration) {
            this.mShimmerDuration = shimmerDuration;
            return this;
        }

        /**
         * @param shimmerColor the shimmer color
         */
        public Builder color(@ColorRes int shimmerColor) {
            this.mShimmerColor = ContextCompat.getColor(listView.getContext(), shimmerColor);
            return this;
        }

        /**
         * @param shimmerAngle the angle of the shimmer effect in clockwise direction in degrees.
         */
        public Builder angle(@IntRange(from = -45, to = 45) int shimmerAngle) {
            this.mShimmerAngle = shimmerAngle;
            return this;
        }

        /**
         * @param skeletonLayoutResID the loading skeleton layoutResID
         */
        public Builder load(@LayoutRes int skeletonLayoutResID) {
            this.mItemResID = skeletonLayoutResID;
            return this;
        }

        /**
         * @param skeletonLayoutResIDs the loading array of skeleton layoutResID
         */
        public Builder loadArrayOfLayouts(@ArrayRes int[] skeletonLayoutResIDs) {
            this.mItemsResIDArray = skeletonLayoutResIDs;
            return this;
        }

        public ListViewSkeletonScreen show() {
            ListViewSkeletonScreen listViewSkeletonScreen = new ListViewSkeletonScreen(this);
            listViewSkeletonScreen.show();
            return listViewSkeletonScreen;
        }
    }
}
